package com.sugon.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.MaintaPlanChangeRecordsDao;
import com.sugon.entity.MaintaPlanChangeRecordsEntity;
import com.sugon.entity.ModuleMaintainPlanEntity;
import com.sugon.service.MaintaPlanChangeRecordsService;
import com.sugon.service.ModuleMaintainPlanService;
import com.sugon.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 维护计划变更记录Service实现类
 *
 * @author sugon
 * @date 2019-09-25 11:44:29
 */
@Service("maintaPlanChangeRecordsService")
public class MaintaPlanChangeRecordsServiceImpl implements MaintaPlanChangeRecordsService {
    @Autowired
    private MaintaPlanChangeRecordsDao maintaPlanChangeRecordsDao;
    @Autowired
    private ModuleMaintainPlanService moduleMaintainPlanService;

    @Override
    public MaintaPlanChangeRecordsEntity queryObject(String id) {
        return maintaPlanChangeRecordsDao.queryObject(id);
    }

    @Override
    public List<MaintaPlanChangeRecordsEntity> queryList(Map<String, Object> map) {
        return maintaPlanChangeRecordsDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return maintaPlanChangeRecordsDao.queryTotal(map);
    }

    @Override
    public int save(MaintaPlanChangeRecordsEntity maintaPlanChangeRecords) {
        maintaPlanChangeRecords.setId(RandomUtil.randomString(32));
        maintaPlanChangeRecords.setCreateTime(DateUtil.date());
        maintaPlanChangeRecords.setDelFlag(1);
        return maintaPlanChangeRecordsDao.save(maintaPlanChangeRecords);
    }

    @Override
    public int update(MaintaPlanChangeRecordsEntity maintaPlanChangeRecords) {
        maintaPlanChangeRecords.setApprovalTime(new Date());
        maintaPlanChangeRecords.setApprover(ShiroUtils.getUserId());
//        maintaPlanChangeRecords.set
        return maintaPlanChangeRecordsDao.update(maintaPlanChangeRecords);
    }

    @Override
    public int delete(String id) {
        return maintaPlanChangeRecordsDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        return maintaPlanChangeRecordsDao.deleteBatch(ids);
    }

    @Override
    public String isApply(String id) {
        return maintaPlanChangeRecordsDao.isApply(id);
    }

    @Override
    public int updateApproval(MaintaPlanChangeRecordsEntity entity) {
        entity.setApprovalTime(new Date());
        entity.setApprover(ShiroUtils.getUserId());
        String status = entity.getApprovalStatus();
        ModuleMaintainPlanEntity moduleMaintainPlanEntity = new ModuleMaintainPlanEntity();
        moduleMaintainPlanEntity.setId(entity.getModelMaintaPlanId());
        if ("1".equals(status)) {//审批通过
            //将审批延期时间更新到计划结束时间
            moduleMaintainPlanEntity.setPlanEndDate(entity.getNewPlanTime());
            moduleMaintainPlanEntity.setIsDelay(2);
            moduleMaintainPlanService.update(moduleMaintainPlanEntity);
        } else {//未通过

            moduleMaintainPlanEntity.setIsDelay(3);
            moduleMaintainPlanService.update(moduleMaintainPlanEntity);
        }
        return maintaPlanChangeRecordsDao.approval(entity);
    }


}
